<?php
class ControllerArticleMessage extends ListController {

    protected function heading_title() {
        return "私信列表";
    }
    protected function primary_key_name() {
        return "ID";
    }
    protected function class_path() {
        return "article/message";
    }
    protected function model_name() {
        return "model_article_message";
    }
    public function preview(){
        $this->load->model($this->class_path());

        $item = $this->model_article_message->getItem($this->request->get['ID']);

        $this->response->setOutput($this->load->view('article/preview.php', $item));
    }

    public function add() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

            $id = $this->{$this->model_name()}->add($this->request->post,$this->request->get);

            switch ($this->request->post['to']) {

                case 'customer_group':
                    $this->{$this->model_name()}->send_to_user_group($id, $this->request->post['customer_group_id'], $this->user->getId(),$this->request->post['send_time'] );
                    break;
                case 'customer':
                    if (!empty($this->request->post['customer'])) {
                        $this->{$this->model_name()}->send_to_users($id, $this->request->post['customer'], $this->user->getId(),$this->request->post['send_time']  );
                    }



                    break;

            }




            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $extra . $url, 'SSL'));



        }

        $this->getForm();
    }

    protected function get_list_data(&$ret,$filter_data,$url)
    {
        $total = $this->{$this->model_name()}->getCount();

        if (isset($this->request->get['filter_title'])) {
            $filter_data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $filter_data['filter_title']  = null;
        }
        if (isset($this->request->get['filter_category'])) {
            $filter_data['filter_category'] = $this->request->get['filter_category'];
        } else {
            $filter_data['filter_category']  = null;
        }

        $ret = $this->{$this->model_name()}->getList($filter_data);

        foreach ($ret as &$result) {

            $result['preview'] = $this->url->link('article/article/preview', 'token=' . $this->session->data['token'] . '&'.$this->primary_key_name(). '=' . $result[$this->primary_key_name()] . $url, 'SSL');
        }

        return $total;
    }

    protected function push_data(&$data){
        $this->load->model('article/term');

        $tmp = array();
        $tmp['taxonomy'] = 'category';
        $data['categories'] = $this->model_article_term->getList($tmp);


        if (isset($this->request->get['filter_title'])) {
            $data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $data['filter_title'] = null;
        }


        if (isset($this->request->get['filter_category'])) {
            $data['filter_category'] = (int)$this->request->get['filter_category'];
        } else {
            $data['filter_category'] = null;
        }




        $status = 'publish';
        if(isset($_GET['status']) && $_GET['status'] == 'draft')
            $status = 'draft';

        $data['status'] = $status;
    }


    protected function get_form_data(&$data){
        $item = null;
        if (isset($this->request->get[$this->primary_key_name()]) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $item = $this->{$this->model_name()}->getItem($this->request->get[$this->primary_key_name()]);
            $data['post_status'] = $item['post_status'];
        }

        $this->load->model('tool/image');

        if (isset($this->request->post['post_title'])) {
            $data['post_title'] = $this->request->post['post_title'];
        } elseif (isset($item)) {
            $data['post_title'] = $item['post_title'];
        } else {
            $data['post_title'] = '';
        }

        if (isset($this->request->post['send_time'])) {
            $data['send_time'] = $this->request->post['send_time'];
        } elseif (isset($item)) {
            $data['send_time'] = $item['send_time'];
        } else {
            $data['send_time'] = '';
        }

        if (isset($this->request->post['template_id'])) {
            $data['template_id'] = $this->request->post['template_id'];
        } elseif (isset($item)) {
            $data['template_id'] = $item['ref_id'];
        } else {
            $data['template_id'] = 0;
        }

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (isset($item)) {
            $data['image'] = $item['image'];
        } else {
            $data['image'] = '';
        }

        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        if (is_file(DIR_IMAGE . $data['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($data['image'], 100, 100);
        }
        else{
            $data['thumb'] = $data['placeholder'];
        }

        if (isset($this->request->post['post_content'])) {
            $data['post_content'] = $this->request->post['post_content'];
        } elseif (isset($item)) {
            $data['post_content'] = $item['post_content'];
        } else {
            $data['post_content'] = '';
        }



        $data['customer_groups'] = $this->{$this->model_name()}->getCustomerGroup();


        $data['templates'] = $this->{$this->model_name()}->getTemplates();


    }


    protected function validateForm() {
        if (!$this->user->hasPermission('modify',$this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }

        if (utf8_strlen($_POST['post_title']) == 0) {
            $this->error['warning'] = '标题不能为空';
            return !$this->error;
        }

        if (utf8_strlen($_POST['send_time']) == 0) {
            $this->error['warning'] = '发送时间不能为空';
            return !$this->error;
        }

        return !$this->error;
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify',$this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }



        return !$this->error;
    }
}